package com.lovo.project.servlet.user;

import com.lovo.project.enums.UserCodeEnum;
import com.lovo.project.exception.OurException;
import com.lovo.project.model.UserDO;
import com.lovo.project.service.UserService;
import com.lovo.project.service.impl.UserServiceImpl;
import com.lovo.project.vo.ResultVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @title 获取session
 * @author: luo
 * @date: 2020/11/9 14:43
 */
@WebServlet(name = "GetSessionServlet",urlPatterns = "/user/getSession")
public class GetSessionServlet extends HttpServlet {

    private UserService userService = new UserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        PrintWriter out = response.getWriter();
        UserDO loginUser = (UserDO) request.getSession().getAttribute("loginUser");
        try {
            if (loginUser == null) {
                out.print(ResultVO.fail(UserCodeEnum.USER_LOGIN_ERROR).toJson());
            }
            out.print(ResultVO.success().add(loginUser).toJson());
        } catch (OurException e) {
            out.print(ResultVO.fail(e.getCodeEnum()).toJson());
        }catch (Exception e1){
            out.print(ResultVO.fail(UserCodeEnum.USER_UNKNOWN_ERROR).toJson());
        }finally {
            out.close();
        }


    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }


}
